﻿using System.Collections.ObjectModel;
using System.IO;
using Workbench.Models;
using Workbench.Operations;
using Workbench.Shared;

namespace Workbench.Presentation
{
    public class ApplicationMenuViewModel : ViewModelBase
    {
        public ApplicationMenuViewModel()
        {
            rebindRecentlyUsed();
            rebindAvaialblePOSTypeSets();
        }

        private ObservableCollection<string> recentlyUsedProjects = new ObservableCollection<string>();
        public ObservableCollection<string> RecentlyUsedProjects { get { return recentlyUsedProjects;  } }

        private ObservableCollection<string> availablePOSTypeSets = new ObservableCollection<string>();
        public ObservableCollection<string> AvailablePOSTypeSets { get { return availablePOSTypeSets; } }

        private bool canCloseProject;
        public bool CanCloseProject
        {
            get { return canCloseProject; }
            set { canCloseProject = value; RaisePropertyChanged(); }
        }

        private bool hasRecentlyUsedProjects;
        public bool HasRecentlyUsedProjects
        {
            get { return hasRecentlyUsedProjects; }
            set { hasRecentlyUsedProjects = value; RaisePropertyChanged(); }
        }

        private bool hasAvailablePOSTypeSets;
        public bool HasAvailablePOSTypeSets
        {
            get { return hasAvailablePOSTypeSets; }
            set { hasAvailablePOSTypeSets = value; RaisePropertyChanged(); }
        }

        public void CreateProject(string projectName, string projectDbPath)
        {
            ProjectOperations.CreateAndOpenProject(projectDbPath, projectName);
            addToRecentlyUsed(projectDbPath);
            CanCloseProject = true;
        }

        public void OpenProject(string projectDbPath)
        {
            if (!File.Exists(projectDbPath))
            {
                throw new IOException("The specified file doesn't exists");
            }
            ProjectOperations.OpenProject(projectDbPath);
            addToRecentlyUsed(projectDbPath);
            CanCloseProject = true;
        }
        public void CloseProject()
        {
            ProjectOperations.CloseProject();
            CanCloseProject = false;
        }
        
        private void addToRecentlyUsed(string projectDbPath)
        {
            UserSettings.MostRecentlyUsedProjects.AddProject(projectDbPath);
            rebindRecentlyUsed();
        }

        public void RemoveFromRecentlyUsed(string projectDbPath)
        {
            UserSettings.MostRecentlyUsedProjects.RemoveProject(projectDbPath);
            rebindRecentlyUsed();
        }

        private void rebindRecentlyUsed()
        {
            recentlyUsedProjects.Clear();
            recentlyUsedProjects.AddRange(UserSettings.MostRecentlyUsedProjects.GetProjects() ?? new string[0]);
            HasRecentlyUsedProjects = recentlyUsedProjects.Count > 0;
        }

        private void rebindAvaialblePOSTypeSets()
        {
            AvailablePOSTypeSets.Clear();
            AvailablePOSTypeSets.AddRange(ProjectOperations.GetAvailablePOSTypeSets());
            HasAvailablePOSTypeSets = AvailablePOSTypeSets.Count > 0;
        }

        public void ImportPOSTypesSet(string setName)
        {
            ProjectOperations.GetPOSTypes().ForEach(x => ProjectOperations.RemovePOSType(x));
            var newPosTypes = ProjectOperations.LoadPOSTypesFromFile(setName);
        }
    }
}
